package org.celery.modules.productSource.async;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
public class AsyncConfig {

    @Bean
    public AsyncTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        // 核心线程
        threadPoolTaskExecutor.setCorePoolSize(60);
        // 最大线程
        threadPoolTaskExecutor.setMaxPoolSize(80);
        // 队列容量
        threadPoolTaskExecutor.setQueueCapacity(120);
        // 设置线程活跃
        threadPoolTaskExecutor.setKeepAliveSeconds(2);
        // 设置线程名
        threadPoolTaskExecutor.setThreadNamePrefix("async-task-");
        // 设置拒绝策略
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 等待任务结束关闭线程池
        threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        return threadPoolTaskExecutor;
    }
}
